import 'package:flutter/material.dart';
import 'package:flutter_douban/pages/home/home_page.dart';

class _Item {
  final String name, activeIcon, normalIcon;

  _Item(this.name, this.activeIcon, this.normalIcon);
}

class ContainerPage extends StatefulWidget {
  ContainerPage({Key key}) : super(key: key);

  @override
  _ContainerPageState createState() => _ContainerPageState();
}

class _ContainerPageState extends State<ContainerPage> {
  final itemNames = [
    _Item('首页', 'assets/images/ic_tab_home_active.png',
        'assets/images/ic_tab_home_normal.png'),
    _Item('书影音', 'assets/images/ic_tab_subject_active.png',
        'assets/images/ic_tab_subject_normal.png'),
    _Item('小组', 'assets/images/ic_tab_group_active.png',
        'assets/images/ic_tab_group_normal.png'),
    _Item('市集', 'assets/images/ic_tab_shiji_active.png',
        'assets/images/ic_tab_shiji_normal.png'),
    _Item('我的', 'assets/images/ic_tab_profile_active.png',
        'assets/images/ic_tab_profile_normal.png')
  ];
  List<Widget> _pages;
  List<BottomNavigationBarItem> _itemList;

  @override
  void initState() {
    super.initState();
    if (_pages == null) {
      _pages = [
        HomePage(),
        Center(
          child: Text("我是第二页"),
        ),
        Center(
          child: Text("我是第三页"),
        ),
        Center(
          child: Text("我是第四页"),
        ),
        Center(
          child: Text("我是第五页"),
        ),
      ];
    }
    if (_itemList == null) {
      _itemList = itemNames
          .map((item) => BottomNavigationBarItem(
                icon: Image.asset(
                  item.normalIcon,
                  width: 30.0,
                  height: 30.0,
                ),
                title: Text(
                  item.name,
                  style: TextStyle(fontSize: 10.0),
                ),
                activeIcon: Image.asset(
                  item.activeIcon,
                  width: 30.0,
                  height: 30.0,
                ),
              ))
          .toList();
    }
  }

  int _selectIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        children: _pages,
        index: _selectIndex,
      ),
      backgroundColor: Color.fromARGB(255, 248, 248, 248),
      bottomNavigationBar: BottomNavigationBar(
        items: _itemList,
        onTap: (int index) {
          setState(() {
            _selectIndex = index;
          });
        },
        iconSize: 24,
        currentIndex: _selectIndex,
        fixedColor: Color.fromARGB(255, 0, 189, 96),
        type: BottomNavigationBarType.fixed,
      ),
    );
  }
}
